﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using DLL;
using NursingFormSystem.Model;

namespace NursingFormSystem.NursingPlanForm
{
    public partial class PlanFormList : DevExpress.XtraEditors.XtraForm
    {
        string[] user;//当前的登录信息
        string[] patientinfo;
        DataTable dt = new DataTable();//初始化相关网格控件
        public PlanFormList(string[] UserParam, string[] patientInfo, string FormName, string FormID)
        {
            InitializeComponent();
            user = UserParam;
            this.patientinfo = patientInfo;
            InitConfig();
            InitData();

        }

        private void InitConfig()
        {
            dateEdit_sdate.Text = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
            dateEdit_edate.Text = DateTime.Now.Date.ToString("yyyy-MM-dd");
            labelControl_patientname.Text = patientinfo[2];//患者信息
            labelControl_Bedno.Text = patientinfo[5];//患者信息
            dt.Columns.Add(new DataColumn("RecordDate", typeof(string)));
            dt.Columns.Add(new DataColumn("RecordTime", typeof(string)));
            dt.Columns.Add(new DataColumn("NursingDigagnose", typeof(string)));
            dt.Columns.Add(new DataColumn("NursingGoal", typeof(string)));
            dt.Columns.Add(new DataColumn("NursingMeasure", typeof(string)));
            dt.Columns.Add(new DataColumn("RecordSign", typeof(string)));
            dt.Columns.Add(new DataColumn("NursingEvaluate", typeof(string)));
            dt.Columns.Add(new DataColumn("StopDate", typeof(string)));
            dt.Columns.Add(new DataColumn("StopTime", typeof(string)));
            dt.Columns.Add(new DataColumn("StopSign", typeof(string)));
            dt.Columns.Add(new DataColumn("Id", typeof(string)));
            gridControl1.DataSource = dt;
        }


        private void ShowMessage()
        {
            //获得数据并且刷新
            InitData();
        }

        private void InitData()
        {
            dt.Clear();
            if (this.patientinfo[0] == null) return;
            int visitid = int.Parse(patientinfo[1]);
            DataSet ds = GetPlanRecord(patientinfo[0], visitid, patientinfo[10]);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt2 = ds.Tables[0];
                foreach (DataRow dr in dt2.Rows)
                {
                    DataRow dr0 = dt.NewRow();
                    dr0["RecordDate"] = dr["RecordDate"];
                    dr0["RecordTime"] = dr["RecordTime"];
                    dr0["NursingDigagnose"] = dr["NursingDigagnose"];
                    dr0["NursingGoal"] = dr["NursingGoal"];
                    dr0["NursingMeasure"] = dr["NursingMeasure"];
                    dr0["RecordSign"] = dr["RecordSign"];
                    dr0["NursingEvaluate"] = dr["NursingEvaluate"];
                    dr0["StopDate"] = dr["StopDate"];
                    dr0["StopTime"] = dr["StopTime"];
                    dr0["StopSign"] = dr["StopSign"];
                    dr0["Id"] = dr["Id"];
                    dt.Rows.Add(dr0);
                }
                gridView1.RefreshData();
            }
            //        DateTime startdate,DateTime enddate,
        }

        /// <summary>
        /// 获取已经有的记录
        /// </summary>
        /// <param name="patientid">患者ID</param>
        /// <param name="visitid">住院ID</param>
        /// <param name="startdate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <param name="wardid">病区ID</param>
        /// <returns></returns>
        private static DataSet GetPlanRecord(string patientid, int visitid, string wardid)
        {

            SqlParameter[] param = new SqlParameter[]{
                new SqlParameter("@PatientId", SqlDbType.NVarChar),
                new SqlParameter("@Visitid", SqlDbType.Int),
                new SqlParameter("@WardId", SqlDbType.NVarChar)
            };
            param[0].Value = patientid;
            param[1].Value = visitid;
            param[2].Value = wardid;
            string sql = "";
            try
            {
                sql = @"use BNNursingDocsSystem_Develop; select * from NursingPlanRecord where [PatientId]=@PatientId and [VisitId]=@Visitid and [WardId]=@WardId";

                var table = SqlHelper.ExecuteDataset(sql, System.Data.CommandType.Text, param);
                return table;
            }
            catch (Exception)
            {
                return null;
            }
        }

        private void simpleButton_add_Click(object sender, EventArgs e)
        {
            AddPlanForm addform = new AddPlanForm(user, patientinfo);
            addform.ShowUpdate += new AddPlanForm.SuccessUpdateDelegate(ShowMessage);
            addform.Text = "新增护理计划";
            addform.Show();
            if (addform.DialogResult == DialogResult.OK)
            {
                InitData();
            }
        }

        //编辑按钮
        private void simpleButton_edit_Click(object sender, EventArgs e)
        {
            if (gridView1.GetFocusedDataRow() != null)
            {
                string date = gridView1.GetFocusedDataRow()["RecordDate"].ToString();
                string time = gridView1.GetFocusedDataRow()["RecordTime"].ToString();
                string diagnose = gridView1.GetFocusedDataRow()["NursingDigagnose"].ToString();
                string goal = gridView1.GetFocusedDataRow()["NursingGoal"].ToString();
                string step = gridView1.GetFocusedDataRow()["NursingMeasure"].ToString();
                string id = gridView1.GetFocusedDataRow()["Id"].ToString();

                AddPlanForm addform = new AddPlanForm(date, time, user, patientinfo, diagnose, goal, step, id);
                addform.ShowUpdate += new AddPlanForm.SuccessUpdateDelegate(ShowMessage);
                addform.Text = "编辑护理计划";
                addform.Show();
            }
        }
        //查询按钮
        private void button_search_Click(object sender, EventArgs e)
        {
            InitData();
        }
        /// <summary>
        /// 打印按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton_print_Click(object sender, EventArgs e)
        {
            int formid = (int)FormType.NursingPlanForm;
            DateTime sdate = Convert.ToDateTime(dateEdit_sdate.Text);
            DateTime edate = Convert.ToDateTime(dateEdit_edate.Text);
            PlanPrint planprint = new PlanPrint(this.patientinfo, formid, sdate, edate);
            planprint.ShowDialog();
        }

        private void simpleButton_stop_Click(object sender, EventArgs e)
        {
            if (gridView1.GetFocusedDataRow() != null)
            {
                string nursingevaluate = gridView1.GetFocusedDataRow()["NursingEvaluate"].ToString();
                string id = gridView1.GetFocusedDataRow()["Id"].ToString();

                StopPlanForm stopform = new StopPlanForm(user, patientinfo, nursingevaluate, id);
                stopform.ShowUpdate1 += new StopPlanForm.SuccessUpdateDelegate(ShowMessage);
                stopform.Show();
            }

        }

        private void simpleButton_delete_Click(object sender, EventArgs e)
        {
            if (gridView1.GetFocusedDataRow() != null)
            {
                if (MessageBox.Show("要删除该条数据吗?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    string id = gridView1.GetFocusedDataRow()["Id"].ToString();
                    string sql = "";
                    sql = "delete from  NursingPlanRecord  WHERE PatientId='" + patientinfo[0] + "' AND VisitId='" + patientinfo[1] + "'AND Id='" + id + "'";
                    if (sql != "")//至少有一条数据
                    {
                        SqlHelper.ExecuteScalar(System.Data.CommandType.Text, sql, null);
                    }
                    MessageBox.Show("删除成功！");
                    InitData();
                }
            }
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.GetFocusedDataRow() != null)
            {
                string date = gridView1.GetFocusedDataRow()["RecordDate"].ToString();
                string time = gridView1.GetFocusedDataRow()["RecordTime"].ToString();
                string diagnose = gridView1.GetFocusedDataRow()["NursingDigagnose"].ToString();
                string goal = gridView1.GetFocusedDataRow()["NursingGoal"].ToString();
                string step = gridView1.GetFocusedDataRow()["NursingMeasure"].ToString();
                string id = gridView1.GetFocusedDataRow()["Id"].ToString();

                AddPlanForm addform = new AddPlanForm(date, time, user, patientinfo, diagnose, goal, step, id);
                addform.Text = "编辑护理计划";
                addform.Show();
                if (addform.DialogResult == DialogResult.OK)
                {
                    InitData();
                }
            }
        }
    }
}